package co.sanduche.teach.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import co.sanduche.teach.gallery.GalleryActivity;
import co.sanduche.teach.gallery.GalleryDBOpenHelper;
import co.sanduche.teach.gallery.ImageItem;
import co.sanduche.util.DialogUtil;

public class ServiceManager {

	private GalleryActivity gallery;
	private TaskExecutorServiceConnectionCallback connection;
	public ServiceBinder serviceBinder;

	public ServiceManager(GalleryActivity galleryActivity) {
		this.gallery = galleryActivity;
	}

	public void connectService() {
		Intent i = new Intent(gallery, TaskExecutorService.class);
		gallery.startService(i);
		this.connection = new TaskExecutorServiceConnectionCallback();
		gallery.bindService(i, connection, Context.BIND_AUTO_CREATE);
	}

	public void disconnectService() {
		gallery.unbindService(connection);
	}

	public ImageItem addPicture(String path) {
		ImageItem ii = new GalleryDBOpenHelper(gallery).insert(path);
		TaskExecutorService.newTask(gallery, UploadPhotoTask.class, ii.getId());
		return ii;
	}

	public static class ServiceBinder extends Binder {

		// private TaskExecutorService service;
		private ServiceManager manager;

		public ServiceBinder(TaskExecutorService taskExecutorService) {
			// this.service = taskExecutorService;
		}

		public void setManager(ServiceManager manager) {
			this.manager = manager;
		}

		public void showMessage(final String msg) {
			if (manager == null)
				return;
			manager.gallery.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					DialogUtil.message(manager.gallery, msg);

				}
			});
		}

		public void alert(final String msg) {
			if (manager == null)
				return;
			manager.gallery.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					DialogUtil.alert(manager.gallery, msg);
				}
			});
		}

		public boolean isConnected() {
			return manager != null;
		}

		public void updateImage(final ImageItem ii) {
			if (manager == null)
				return;
			ImageItem local = manager.gallery.customGridAdapter.getItemById(ii
					.getId());
			local.setServerId(ii.getServerId());

			manager.gallery.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					manager.gallery.customGridAdapter.notifyDataSetChanged();
				}
			});
		}

		public void showUploading(final boolean uploading, ImageItem ii) {
			if (manager == null)
				return;
			ImageItem local = manager.gallery.customGridAdapter.getItemById(ii
					.getId());
			local.setServerId(ii.getServerId());
			local.setState(ii.getState());

			manager.gallery.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					manager.gallery.showProgressBar(uploading);
					manager.gallery.customGridAdapter.notifyDataSetChanged();
				}
			});
		}
	}

	public class TaskExecutorServiceConnectionCallback implements
			ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder binder) {
			ServiceManager.this.serviceBinder = (ServiceBinder) binder;
			// ServiceManager.this.service = serviceBinder.service;
			serviceBinder.setManager(ServiceManager.this);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// ServiceManager.this.service = null;
			serviceBinder.setManager(null);
		}

	}
}
